package cn.tedu.spotms.server.controller;

import cn.tedu.spotms.server.pojo.dto.RoleAddNewDTO;
import cn.tedu.spotms.server.pojo.dto.RoleMatchPermissionsDTO;
import cn.tedu.spotms.server.pojo.vo.RoleListItemVO;
import cn.tedu.spotms.server.service.IRoleService;
import cn.tedu.spotms.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/roles",produces = "application/json;charset=utf-8")
@Api(tags = "角色模块")
public class RoleController {

    @Autowired
    private IRoleService service;

    @PostMapping("/add-new")
    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 10)
    @PreAuthorize("hasAuthority('/role/update')")
    public JsonResult<Void> addNew(RoleAddNewDTO roleAddNewDTO){
        service.addNew(roleAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/match-permissions")
    @ApiOperation("配置权限")
    @ApiOperationSupport(order = 20)
    @PreAuthorize("hasAuthority('/role/update')")
    public JsonResult<Void> matchPermissions(RoleMatchPermissionsDTO roleMatchPermissionsDTO) {
        service.matchPermissions(roleMatchPermissionsDTO);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("角色列表")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/role/read')")
    public JsonResult<List<RoleListItemVO>> list(){
        List<RoleListItemVO> list = service.list();
        return JsonResult.ok(list);
    }
}
